package com.gr3g.dev.engine.model;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonReader;

import org.lwjgl.opengl.GL11;

import com.gr3g.dev.engine.exception.GameException;

/** @author Gregory Tardivel */
public final class JsonLoader {

    public static void drawQuad(final String file)
            throws GameException {
        try {
            final InputStream in = new FileInputStream(file);
            final JsonReader reader = Json.createReader(in);
            final JsonObject objet = reader.readObject();

            final JsonObject quad = objet.getJsonObject("quad");
            final String pX1 = quad.getString("x1");
            final String pX2 = quad.getString("x2");
            final String pX3 = quad.getString("x3");
            final String pY1 = quad.getString("y1");
            final String pY2 = quad.getString("y2");
            final String pY3 = quad.getString("y3");
            final String pZ1 = quad.getString("z1");
            final String pZ2 = quad.getString("z2");
            final String pZ3 = quad.getString("z3");

            final JsonObject color = objet.getJsonObject("color");
            final String valueR = color.getString("r");
            final String valueG = color.getString("g");
            final String valueB = color.getString("b");
            final String valueA = color.getString("a");

            reader.close();
            in.close();

            GL11.glColor4f(Float.parseFloat(valueR), Float.parseFloat(valueG),
                    Float.parseFloat(valueB), Float.parseFloat(valueA));
            GL11.glBegin(GL11.GL_QUADS);

            GL11.glVertex3f(Float.parseFloat(pX1), Float.parseFloat(pY1), Float
                    .parseFloat(pZ1));
            GL11.glVertex3f(Float.parseFloat(pX2), Float.parseFloat(pY1), Float
                    .parseFloat(pZ1));
            GL11.glVertex3f(Float.parseFloat(pX2), Float.parseFloat(pY2), Float
                    .parseFloat(pZ1));
            GL11.glVertex3f(Float.parseFloat(pX1), Float.parseFloat(pY2), Float
                    .parseFloat(pZ1));

            GL11.glVertex3f(Float.parseFloat(pX1), Float.parseFloat(pY1), Float
                    .parseFloat(pZ1));
            GL11.glVertex3f(Float.parseFloat(pX1), Float.parseFloat(pY2), Float
                    .parseFloat(pZ1));
            GL11.glVertex3f(Float.parseFloat(pX1), Float.parseFloat(pY2), Float
                    .parseFloat(pZ2));
            GL11.glVertex3f(Float.parseFloat(pX1), Float.parseFloat(pY1), Float
                    .parseFloat(pZ2));

            GL11.glVertex3f(Float.parseFloat(pX1), Float.parseFloat(pY1), Float
                    .parseFloat(pZ1));
            GL11.glVertex3f(Float.parseFloat(pX3), Float.parseFloat(pY1), Float
                    .parseFloat(pZ1));
            GL11.glVertex3f(Float.parseFloat(pX3), Float.parseFloat(pY3), Float
                    .parseFloat(pZ1));
            GL11.glVertex3f(Float.parseFloat(pX1), Float.parseFloat(pY3), Float
                    .parseFloat(pZ1));

            GL11.glVertex3f(Float.parseFloat(pX1), Float.parseFloat(pY1), Float
                    .parseFloat(pZ1));
            GL11.glVertex3f(Float.parseFloat(pX1), Float.parseFloat(pY1), Float
                    .parseFloat(pZ3));
            GL11.glVertex3f(Float.parseFloat(pX1), Float.parseFloat(pY3), Float
                    .parseFloat(pZ3));
            GL11.glVertex3f(Float.parseFloat(pX1), Float.parseFloat(pY3), Float
                    .parseFloat(pZ1));

            GL11.glEnd();
        } catch (final IOException exc) {
            GameException.error("Modele non chargé", exc, false);
        }

    }
}
